package com.kun.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kun.product.entity.ClassifyEntity;
import com.kun.product.service.ClassifyService;
import com.kun.common.utils.PageUtils;
import com.kun.common.utils.R;



/**
 * 
 *
 * @author kun_er
 * @email 2661038743@qq.com
 * @date 2022-05-02 16:11:15
 */
@RestController
@RequestMapping("product/classify")
public class ClassifyController {
    @Autowired
    private ClassifyService classifyService;

    /**
     * 查询分类不带分页
     */
    @GetMapping("getAllClassify")
    public R getAllClassify(){
        List<ClassifyEntity> list = classifyService.list(null);
        return R.ok().put("data",list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = classifyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		ClassifyEntity classify = classifyService.getById(id);

        return R.ok().put("classify", classify);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ClassifyEntity classify){
		classifyService.save(classify);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ClassifyEntity classify){
		classifyService.updateById(classify);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){

		boolean b =  classifyService.removeClassifyByIds(Arrays.asList(ids));
        if (b){
            return R.ok();
        }else {
            return R.error().put("msg","您所选的分类中还有商品，请先删除商品");
        }

    }

}
